package com.fengdi.book.easy;

import com.fengdi.publicObject.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


public class TreeSolution {
    public static class MaxDepth {
        public int maxDepth(TreeNode root) {
            if (root == null) return 0;
            return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
        }
    }

    public static class IsValidBST {
        public ArrayList<Integer> list = new ArrayList<>();

        // 利用BST的性质，BST的中序遍历序列是一个有序的增序列
        public boolean isValidBST(TreeNode root) {
            if (root == null) return true;
            preOrder(root);

            if(list.size() == 1) return true;
            for(int i = 1; i < list.size(); i++) {
                if(list.get(i - 1) < list.get(i)) continue;
                return false;
            }
            return true;
        }

        public void preOrder(TreeNode root) {
            if(root == null) return;
            preOrder(root.left);
            list.add(root.val);
            preOrder(root.right);
        }
    }

    public static class IsSymmetric {
        public boolean isSymmetric(TreeNode root) {
            if (root == null) return true;

            return isSymmetricHelper(root.left, root.right);
        }

        public boolean isSymmetricHelper(TreeNode left, TreeNode right) {
            if(left == null && right == null) return true;

            if(left == null || right == null || left.val != right.val) return false;
            return isSymmetricHelper(left.left, left.right) && isSymmetricHelper(right.left, right.right);
        }
    }

    public static class LevelOrder {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> result = new ArrayList<>();

        public List<List<Integer>> levelOrder(TreeNode root) {
            if(root == null) return new ArrayList<>();

            queue.add(root);

            while(!queue.isEmpty()) {
                int level = queue.size();
                List<Integer> levelList = new ArrayList<>();
                for(int i = 0; i < level; i++) {
                    TreeNode node = queue.poll();
                    levelList.add(node.val);
                    if(node.left != null) queue.add(node.left);
                    if(node.right != null) queue.add(node.right);
                }
                result.add(levelList);
            }
            return result;
        }
    }

    public static class SortedArrayToBST {
        public TreeNode sortedArrayToBST(int[] num) {
            if (num.length == 0)
                return null;
            return sortedArrayToBST(num, 0, num.length - 1);
        }

        public TreeNode sortedArrayToBST(int[] num, int start, int end) {
            if (start > end)
                return null;
            int mid = (start + end) >> 1;
            TreeNode root = new TreeNode(num[mid]);
            root.left = sortedArrayToBST(num, start, mid - 1);
            root.right = sortedArrayToBST(num, mid + 1, end);
            return root;
        }
    }
}
